package com.bytesizedhosting.api.android.darthcrap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public abstract class ExtendedActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blank);
    }
	
	@Override
	public void setContentView(int intLayoutResID) {
		super.setContentView(intLayoutResID);
		showBytesizedBackground();
	}
	
	@Override
	public void setContentView(View objView) {
		super.setContentView(objView);
		showBytesizedBackground();
	}
	
	public void setContentView(View objView, LayoutParams objParams) {
		super.setContentView(objView, objParams);
		showBytesizedBackground();
	}
	
	public View getContentView() {
		return ExtendedActivity.getContentView(this);
	}
	
	public static View getContentView(Activity objActivity) {
		return ((ViewGroup)objActivity.findViewById(android.R.id.content)).getChildAt(0);
	}
	
	private void showBytesizedBackground() {
		View objCurrentView = getContentView();
		BitmapDrawable objBackground = new BitmapDrawable(BitmapFactory.decodeResource(this.getResources(), R.drawable.background));
		objBackground.setTileModeXY(Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
		objCurrentView.setBackgroundDrawable(objBackground);
	}
	
	public void restartActivity() {
	    Intent objIntent = getIntent();
	    overridePendingTransition(0, 0);
	    objIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
	    finish();

	    overridePendingTransition(0, 0);
	    startActivity(objIntent);
	}
	
	public abstract class IntentConfigurator {
		public abstract void configure(Intent objIntent);
	}
	
	public void switchTo(Class<?> objClass, boolean boolCloseThis, boolean boolWithResult) {
		switchTo(objClass,boolCloseThis,boolWithResult,null);
	}
	
	public void switchTo(Class<?> objClass, boolean boolCloseThis, int intRequestCode) {
		switchTo(objClass,boolCloseThis,intRequestCode,null);
	}
	
	public void switchTo(Class<?> objClass, boolean boolCloseThis, boolean boolWithResult, IntentConfigurator objConfigurator) {
		switchTo(objClass,boolCloseThis,boolWithResult,0,objConfigurator);
	}
	
	public void switchTo(Class<?> objClass, boolean boolCloseThis, int intRequestCode, IntentConfigurator objConfigurator) {
		switchTo(objClass,boolCloseThis,true,intRequestCode,objConfigurator);
	}
	
	private void switchTo(Class<?> objClass, boolean boolCloseThis, boolean boolWithResult, int intRequestCode, IntentConfigurator objConfigurator) {
		Intent objIntent = new Intent(getContentView().getContext(),objClass);
		if (objConfigurator != null) {
			objConfigurator.configure(objIntent);
		}
		if (boolCloseThis) {
			finish();
		}
		if (boolWithResult) {
			startActivityForResult(objIntent, intRequestCode);
		}
		else {
			startActivity(objIntent);
		}
	}
}